var channelChart;

function initChannelChart() {
	channelChart = right_chart_layout.attachChart({
		view 		: 'spline', //spline = 곡선, line = 직선
		container 	: 'chart_container',
		value 		: '#curVal#',
		label 		: '#eventTime#',
		tooltip 	: '#eventTimeFull#',
		item : {
			borderColor : '#ffffff',
			color : '#000000'
		},
		line : {
			color : '#ff9900',
			width : 2
		},
		xAxis : {
			title : 'EventTime',
			template : '#eventTime#',
		},
		yAxis : {
			title : 'Current Values',
			start : 0,
			end : 100,
			step : 20
		},
	});
	window.setInterval('createChart(false)', 3000);
}

function createChart(isFirst) {
	//		channelChart.clearAll();
	var id 		= 0;
	var count 	= 1;
	
	if ( isFirst ) {
		count = 10;
	}
	
	id = dataStore.get('chartId');
	
	if ( id > 0 ) {
		var tableRequest = $.ajax({
			url : '/bwf/channel_history/' + id + '?count=' + count,
			type : 'GET',
			dataType : 'json',
		});
		tableRequest.done(function(info) {
			//			channelChart.parse(info, 'json');

			var chartData = [];
			var count = info.length;

			for (var i = count - 1; i >= 0; i--) {
				data = info[i];

				var dateTime = new Date(data['eventTime']);
				dateTimeFormat = dateTime.getMinutes() + ':'
						+ dateTime.getSeconds();
				channelChart.add({
					eventTime : dateTimeFormat,
					curVal : data['curVal'],
					eventTimeFull : data['eventTime']
				});

				var dataCount = channelChart.dataCount();

				if (dataCount > 10) {
					channelChart.remove(channelChart.first());
				}
			}

			channelChart.parse(chartData, 'json');
		});
		tableRequest.fail(function(jqXHR, textStatus) {
			//			alert('Request failed!');
		});
	}
}

function channelChartSearch(itemId) {
	if ( itemId == 'a' ) {
		var dhxWins= new dhtmlXWindows();

		var chartWin = dhxWins.createWindow('chartSubWin', 205, 160, 460, 90);
		//chartWin.attachLayout('1C');
		chartWin.setText('검색');
//		chartWin.button('close').show();  
		chartWin_formData = [ { type:'settings', labelWidth:80, inputWidth:250, position:'absolute'  },
			              	  { type:'label', name:'label_from', label:'From', width:75, labelWidth:75, labelAlign:'left', labelLeft:5, labelTop:8  },
			              	  { type:'calendar', name:'calendar_from', inputWidth:120, dateFormat: '%Y-%m-%d %H:%i', enableTime: true, inputLeft:50, inputTop:10  },
			            	  { type:'label', name:'label_to', label:'To', width:75, labelWidth:75, labelAlign:'left', labelLeft:190, labelTop:8  },
			            	  { type:'calendar', name:'calendar_to', inputWidth:120, dateFormat: '%Y-%m-%d %H:%i', enableTime: true, inputLeft:220, inputTop:10  },
			            	  { type:'button', name:'search', label:'검색', value:'검색', inputWidth:100, inputLeft:360, inputTop:8  } ];

		var chartWin_form = chartWin.attachForm(chartWin_formData);
		chartWin_form.attachEvent('onButtonClick', function(name){
			if ( name == 'search' ) {
				var fromDateValue 	= chartWin_form.getInput('calendar_from').value;
				var toDateValue 	= chartWin_form.getInput('calendar_to').value;
				var id 				= dataStore.get('chartId');

				console.log(id);
				createChart(id, 10, fromDateValue + ':00', toDateValue + ':00');
				dhxWins.window('chartSubWin').close();
			}
		});
	}
}